﻿Imports TepcelDominio
Imports TepcelBL
Public Class frmProveedor

    Dim err As New System.Windows.Forms.ErrorProvider
    Dim listaProveedor As New List(Of Proveedor)
    Dim ProveedorBL As New ProveedorBL
    Private Sub frmProveedor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call CargarGrid()
        txtBuscar.Focus()
    End Sub

    Private Sub clearError()
        err.SetError(txtNombreProveedor, "")

    End Sub

    Private Sub CargarGrid()
        DGVLista.Rows.Clear()
        listaProveedor = ProveedorBL.GetAllFromProveedor
        If listaProveedor.Count = 0 Then
            btnModificar.Enabled = False
            btnEliminar.Enabled = False
        End If
        Dim i As Integer
        For i = 0 To listaProveedor.Count - 1
            With listaProveedor.Item(i)
                DGVLista.Rows.Add(i, .codigoProveedor, .nombreProveedor)
            End With
        Next
    End Sub

    Private Sub nuevo()
        btnModificar.Enabled = False
        btnEliminar.Enabled = False
        btnCancelar.Enabled = True
        btnGuardar.Enabled = True
        btnNuevo.Enabled = False

        txtNombreProveedor.Enabled = True

        DGVLista.Enabled = False

        txtNombreProveedor.Focus()
    End Sub

    Private Sub cancelar()
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
        btnCancelar.Enabled = False
        btnGuardar.Enabled = False
        btnNuevo.Enabled = True

        txtNombreProveedor.Enabled = False

        DGVLista.Enabled = True

        txtCodigoProveedor.Text = ""
        txtNombreProveedor.Text = ""

    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        If txtBuscar.Text.Trim.Length = 0 Then
            Call CargarGrid()
        Else
            DGVLista.Rows.Clear()
            listaProveedor = ProveedorBL.SearchFromProveedor(txtBuscar.Text.Trim)
            Dim i As Integer
            For i = 0 To listaProveedor.Count - 1
                With listaProveedor.Item(i)
                    DGVLista.Rows.Add(i, .codigoProveedor, .nombreProveedor)
                End With
            Next
        End If
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Call nuevo()
        Call clearError()
        Dim item As Integer = DGVLista.Item(0, DGVLista.CurrentRow.Index).Value
        With listaProveedor.Item(item)
            txtCodigoProveedor.Text = .codigoProveedor
            txtNombreProveedor.Text = .nombreProveedor
        End With

        txtNombreProveedor.Enabled = True


    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If txtNombreProveedor.Text.Trim.Length = 0 Then
            err.SetError(txtNombreProveedor, "LLene el nombre del motivo de traslado")
            Return
        Else
            err.SetError(txtNombreProveedor, "")
        End If

        If (txtCodigoProveedor.Text.Trim.Length > 0) Then
            Dim mot As New TepcelDominio.Proveedor(txtCodigoProveedor.Text, txtNombreProveedor.Text.Trim)
            txtCodigoProveedor.Text = ProveedorBL.NuevoProveedor(mot)
        Else
            Dim mot As New TepcelDominio.Proveedor("", txtNombreProveedor.Text.Trim)
            txtCodigoProveedor.Text = ProveedorBL.NuevoProveedor(mot)
        End If


        Call cancelar()


        Call CargarGrid()

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim result As DialogResult
        result = MessageBox.Show("confirma que desea Eliminar el Registro seleccionado?", "Eliminar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If result = DialogResult.OK Then
            If ProveedorBL.DeleteProveedor(listaProveedor.Item(CInt(DGVLista.Item(0, DGVLista.CurrentRow.Index).Value)).codigoProveedor) Then
                MessageBox.Show("Registro Eliminado Correctamente", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call CargarGrid()
            Else
                MessageBox.Show("No se elimino el registro seleccionado", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Call cancelar()
        Call clearError()

        txtBuscar.Focus()

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        Call nuevo()
        Call clearError()
    End Sub
End Class